﻿using AutoMapper;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.Error;
using B.S.BasicData.Infrastructure.InProfaces;
using B.S.BasicData.Read.Api.Application.Command.ProductsCommand;
using B.S.BasicData.Read.Api.DTO;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System;

namespace B.S.BasicData.Read.Api.Application.CommandHander.ProductsCommandHander
{
    public class ProductsQueryCommandHander : IRequestHandler<ProductsQueryCommand, ApiPaging<ProductsDTO>>
    {
        private readonly IBaseRepository<Products> ProductsRepository;
        private readonly IBaseRepository<Shop> ShopRepository;
        private readonly IMapper mapper;

        public ProductsQueryCommandHander(IBaseRepository<Products> productsRepository, IBaseRepository<Shop> shopRepository, IMapper mapper)
        {
            ProductsRepository = productsRepository;
            ShopRepository = shopRepository;
            this.mapper = mapper;
        }

        public Task<ApiPaging<ProductsDTO>> Handle(ProductsQueryCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ProductsDTO> result = new ApiPaging<ProductsDTO>();

            var item = from a in ProductsRepository.GetAll()
                    join b in ShopRepository.GetAll() on a.ShopId equals b.ShopId
                    where a.IsDel == false
                    select new ProductsDTO
                    {
                        ProductsId = a.ProductsId,
                        ShopId = a.ShopId,
                        ShopName = b.ShopName,
                        ProductionBatch = a.ProductionBatch,
                        ShelfLife = a.ShelfLife,
                        LicenseNumber = a.LicenseNumber,
                        Manufacturers = a.Manufacturers,
                        CoverImage = a.CoverImage,
                        CreateName = a.CreateName,
                        CreateTime = a.CreateTime,
                        UpdateName = a.UpdateName,
                        UpdateTime = a.UpdateTime,
                        IsDel = a.IsDel,
                    };
            if (!string.IsNullOrEmpty(request.ShopName))
            {
                item = item.Where(x => x.ShopName.Contains(request.ShopName));
            }
            var TotalCount = item.Count();
            var TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            var list = item.OrderByDescending(x => x.ProductsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

        

            var map = mapper.Map<List<ProductsDTO>>(list);

            if (item != null)
            {
                result.Code = ApiEnum.success;
                result.Msg = "查询成功";
                result.list = map.ToList();
                result.TotalCount = TotalCount;
                result.TotalPage = TotalPage;
            }
            else
            {
                result.Code = ApiEnum.error;
                result.Msg = "查询失败";
                result.list = map.ToList();
            }
            return Task.FromResult(result);
        }
    }
}
